package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.pojo.Pages;
import com.subaiwang.service.toolPlatform.ToolPlatformService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("toolPlatform")
public class ToolPlatformController {
    @Resource
    private ToolPlatformService toolPlatformService;

    /**
     * 平台工具管理  --快递物流平台管理
     * @return
     */
    @RequestMapping("/Amounts.html")
    public String Amounts(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=toolPlatformService.ToolPlatformCount(null, null);
        pages.setPageCount(count);
        toolPlatformService.getToolPlatform(null,null,pages);
        session.setAttribute("list",pages);
        return "Amounts";
    }

    /**
     * 平台工具管理  --快递物流平台管理  分页
     * @return
     */
    @RequestMapping(value = "/Amountsajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doGet(HttpSession session,
                        @RequestParam(value = "typename",required = false) String typename,
                        @RequestParam(value = "content",required = false) String content,
                        @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=toolPlatformService.ToolPlatformCount(typename, content);
        if (typename.equals("0")){
            count=toolPlatformService.ToolPlatformCount(null, content);
            toolPlatformService.getToolPlatform(null,content,pages);
        }else{
            count=toolPlatformService.ToolPlatformCount(typename, content);
            toolPlatformService.getToolPlatform(typename,content,pages);
        }
        pages.setPageCount(count);

        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }
}
